package com.deer.wms.base.system.web.factory;

import com.deer.wms.base.system.model.factory.Factory;
import com.deer.wms.base.system.service.factory.FactoryService;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 工厂设置
 */
@Controller
@RequestMapping("/system/factory")
public class FactoryController extends BaseController {

    private String prefix = "system/factory";

    @Autowired
    private FactoryService factoryService;

    @RequiresPermissions("system:factory:view")
    @GetMapping()
    public String itemInfo()
    {
        return prefix + "/factory";
    }

//    /**
//     * 根据条件查询工厂列表信息
//     * @param factoryName
//     * @return
//     */
//    @RequiresPermissions("system:factory:list")
//    @PostMapping("/findList")
//    @ResponseBody
//    public TableDataInfo findListByParams(String factoryName){
//        startPage();
//        List<Factory> list = factoryService.findListByName(factoryName);
//        return getDataTable(list);
//    }

    /**
     * 查询工厂列表信息
     * @param factory
     * @return
     */
    @RequiresPermissions("system:factory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findList(Factory factory){
        startPage();
        List<Factory> list = factoryService.findList(factory);
        return getDataTable(list);
    }

    /**
     * 新增工厂
     * @return
     */
    @GetMapping("/add")
    public String add(){
        return prefix + "/add";
    }

    /**
     * 新增保存
     * @return
     */
    @RequiresPermissions("system:factory:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Factory factory){
        factory.setCreateTime(new Date().toString());
        return toAjax(factoryService.addFactory(factory));
    }

    /**
     * 修改工厂信息
     * @return
     */
    @GetMapping("/edit/{factoryId}")
    public String edit(@PathVariable("factoryId") Integer factoryId, ModelMap mmap){
        Factory factory = factoryService.selectFactoryById(factoryId);
        mmap.put("factory",factory);
        return prefix + "/edit";
    }

    /**
     * 修改保存工厂信息
     * @return
     */
    @RequiresPermissions("system:factory:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Factory factory){
        return toAjax(factoryService.updateFactory(factory));
    }

    /**
     * 删除工厂信息
     * @return
     */
    @RequiresPermissions("system:factory:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeAll(String ids){
        return toAjax(factoryService.removeAll(ids));
    }
}
